package com.lzm.wx.controller;

import com.lzm.bean.*;
import com.lzm.mvc.ResponseBody;
import com.lzm.mvc.ResponseView;
import com.lzm.service.ExpressService;
import com.lzm.util.DateFormatUtil;
import com.lzm.util.JSONUtil;
import com.lzm.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.net.JarURLConnection;
import java.net.http.HttpRequest;

public class QRCodeController {

    @ResponseView("/wx/createQRCode.do")
    public String createQrcode(HttpServletRequest request, HttpServletResponse response){
        String code = request.getParameter("code");
        //express | user
        String type = request.getParameter("type");
        String userPhone = null;
        String qRCodeContent = null;
        if("express".equals(type)){
            //快递二维码：被扫后，展示单个快递信息
            //code
            qRCodeContent = "express_"+code;
        }else {
            //用户二维码：被扫后，快递员（柜子）展示用户所有快递
            //userPhone
            Object temp = UserUtil.getWxUser(request.getSession());
            if (temp instanceof User){
                User wxUser = (User) temp;
                userPhone = wxUser.getUserPhone();
            }else if (temp instanceof Courier){
                Courier wxUser = (Courier) temp;
                userPhone = wxUser.getCourierPhone();
            }
            qRCodeContent = "userPhone_"+userPhone;
        }
        HttpSession session = request.getSession();
        session.setAttribute("qrcode",qRCodeContent);
        return "/personQRcode.html";

    }
       @ResponseBody("/wx/getQRCode.do")
    public String getQRCode(HttpServletRequest request, HttpServletResponse response){
           HttpSession session = request.getSession();
           String qrcode = (String) session.getAttribute("qrcode");
           Message msg = new Message();
           if(qrcode == null){
               msg.setStatus(-1);
               msg.setResult("取件码获取出错，请用户重新操作");
           }else{
               msg.setStatus(0);
               msg.setResult(qrcode);
           }
           return JSONUtil.toJSON(msg);
       }

       @ResponseBody("/wx/updateStatus.do")
       public String updateExpressStatus(HttpServletRequest request, HttpServletResponse response){
           String code = request.getParameter("code");
           boolean flag = ExpressService.updateStatus(code);
           Message msg = new Message();
           if(flag){
               msg.setStatus(0);
               msg.setResult("取件成功");
           }
           else {
               msg.setStatus(-1);
               msg.setResult("取件码不存在，请用户更新二维码");
           }
           String json = JSONUtil.toJSON(msg);
           return json;
       }

       @ResponseBody("/wx/findeExpressByCode.do")
       public String findExpressByCode(HttpServletRequest request, HttpServletResponse response){
           String code = request.getParameter("code");
           Express e = ExpressService.findByCode(code);
           BootStrapTableExpress e2 = null;
           if(e!=null){
               e2 = new BootStrapTableExpress(e.getId(),e.getNumber(),e.getUsername(),e.getUserPhone(),e.getCompany(),code, DateFormatUtil.format(e.getInTime()),e.getOutTime()==null?"未出库":DateFormatUtil.format(e.getOutTime()),e.getStatus()==0?"待取件":"已取件",e.getSysPhone());
           }
           Message msg = new Message();
           if(e == null){
               msg.setStatus(-1);
               msg.setResult("取件码不存在");
           }else {
               msg.setStatus(0);
               msg.setResult("查询成功");
               msg.setData(e2);
           }
           return JSONUtil.toJSON(msg);
       }
}
